<?php
class StoreAppController extends AppController {

	var $transaction;
    var $components = array( 'Currency');
	
	 
	public function beforeFilter(){		
		parent::beforeFilter();	

		// pobranie kursów z NBP
//		$this->Currency->refresch();
		// konfiguracja walut
		$this->Currency->set();

		$this->FileUpload->fileModel('StoreUpload');
		$this->FileUpload->uploadDir(UPLOAD_PATH);		
		
		// model's DataSource object for sql transactions
		$this->transaction = ConnectionManager::getDataSource('default');
		
	}
	
	/**
	 * 
	 * Przekształcenie struktury Tree do wyświetlaneia w combobox lub na liscie
	 * @param array $data // dane z zapytania $model->find('treahend');
	 * @param string $model // nazwa modeluz którego pobrano dane
	 * @param bool $full // true: zwraca array dodając jedynie wartosc depth; false: zwaraca listę id => name; 
	 */
	protected function treeForCombobox($data,$model,$full=false){
		$parentCategories_ = $this->treeComboBox($data,$model);
		if($full) return $parentCategories_;
		$parentCategories = array();
        foreach ($parentCategories_ as $key => $value) {
        	$parentCategories[$value[$model]['id']] = $value[$model]['name'];
        }
        return $parentCategories;
	}
	

	/**
	 * 
	 * Dodaje wartość depth
	 * @param $data
	 * @param $model
	 */
	private function treeComboBox($data,$model) {
        $result = array();
        foreach ($data as $i) {
            $newData = $i;
            $newData[$model]['depth'] = count($this->{$model}->getpath($i[$model]['id'])) - 1;
            $newData[$model]['name'] = str_repeat(" . . . . . . . ", $newData[$model]['depth']).$newData[$model]['name'];
            
            unset($newData['children']);
            $result[] = $newData;
            if(isset($i['children']) && is_array($i['children'])) {
                $children = $this->treeComboBox($i['children'],$model);
                foreach($children as $k) {
                    $result[] = $k;
                }
            }
        }
        return $result;
    }
}
?>